﻿using UnityEngine;

namespace CGF
{
    [RequireComponent(typeof(Camera))]
    [RequireComponent(typeof(CharacterController))]
    public class CameraWalk : MonoBehaviour
    {
        public enum Method
        {
            KeyboardAndLeftMouse,
            KeyboardAndRightMouse,
            FreeFly
        }
        public Method method = Method.KeyboardAndLeftMouse;
        public float moveSpeed = 2;
        public float rotateSpeed = 300;
        public bool useRigidbody = true;

        float defautY;
        CharacterController controller;
        Vector3 velocity;

        void Awake()
        {
            GetComponent<Camera>().nearClipPlane = 0.1f;

            controller = GetComponent<CharacterController>();
            if (method == Method.FreeFly)
            {
                useRigidbody = false;
            }
            controller.enabled = useRigidbody;
            controller.height = 1.7f;
            controller.radius = 0.2f;
            controller.center = new Vector3(0f, -0.5f, 0f);
        }

        void Update()
        {
            switch (method)
            {
                case Method.KeyboardAndLeftMouse:
                case Method.KeyboardAndRightMouse:
                    MoveUpdate();
                    break;
                case Method.FreeFly:
                    FreeFly();
                    break;
            }
        }

        void MoveUpdate()
        {
            bool canRotate = method == Method.KeyboardAndLeftMouse ? Input.GetMouseButton(0) : Input.GetMouseButton(0);
            if (canRotate)
            {
                //左右看
                transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime, 0, Space.World);
                //上下看
                transform.Rotate(-Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime, 0, 0, Space.Self);
            }

            if (useRigidbody)
            {
                //移动
                velocity.x = 0;
                velocity.y = 0;
                velocity.z = 0;
                if (Input.GetKey(KeyCode.W))
                {
                    velocity = transform.forward * moveSpeed;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    velocity -= transform.forward * moveSpeed;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    velocity -= transform.right * moveSpeed;
                }
                else if (Input.GetKey(KeyCode.D))
                {
                    velocity += transform.right * moveSpeed;
                }
                controller.SimpleMove(velocity);
            }
            else
            {
                //移动
                if (Input.GetKey(KeyCode.W))
                {
                    Vector3 pos = transform.position;
                    defautY = pos.y;
                    pos += transform.forward * moveSpeed * Time.deltaTime;
                    pos.y = defautY;
                    transform.position = pos;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    Vector3 pos = transform.position;
                    defautY = pos.y;
                    pos -= transform.forward * moveSpeed * Time.deltaTime;
                    pos.y = defautY;
                    transform.position = pos;
                }
                if (Input.GetKey(KeyCode.A))
                {
                    Vector3 pos = transform.position;
                    defautY = pos.y;
                    pos -= transform.right * moveSpeed * Time.deltaTime;
                    pos.y = defautY;
                    transform.position = pos;
                }
                else if (Input.GetKey(KeyCode.D))
                {
                    Vector3 pos = transform.position;
                    defautY = pos.y;
                    pos += transform.right * moveSpeed * Time.deltaTime;
                    pos.y = defautY;
                    transform.position = pos;
                }
            }
        }

        void FreeFly()
        {
            if (Input.GetMouseButton(0))
            {
                //左右看
                transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime, 0, Space.World);
                //上下看
                transform.Rotate(-Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime, 0, 0, Space.Self);
            }

            //移动
            if (Input.GetKey(KeyCode.W))
            {
                transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
            }
            else if (Input.GetKey(KeyCode.S))
            {
                transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
            }
            if (Input.GetKey(KeyCode.A))
            {
                transform.Translate(Vector3.left * moveSpeed * Time.deltaTime);
            }
            else if (Input.GetKey(KeyCode.D))
            {
                transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
            }
        }
    }
}